<!-- templates/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SERP Analysis Tool</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css" rel="stylesheet">
</head>
<body>

    <div class="container mt-5">
        <h1 class="mb-4">SERP Analysis Tool</h1>
        <div class="mb-3">
            <input type="text" class="form-control" id="keyword" placeholder="Enter keyword">
            <button class="btn btn-primary mt-2" id="search">Search</button>
            <a href="/static/serp.html" target="_blank" class="btn btn-secondary mt-2">View Last Scraped SERP</a>
            <a href="/static/ppa.html" target="_blank" class="btn btn-secondary mt-2">View Last Scraped PAA</a>
        </div>
        <div class="row">
            <div class="col-md-12">
                <h3>SERP Results</h3>
                <textarea class="form-control" id="serpResults" rows="10" readonly></textarea>
            </div>
        </div>

        <div class="mt-4">
            <h3>People Also Ask (Q & A)</h3>
            <textarea class="form-control" id="paaResults" rows="10" readonly></textarea>
        </div>

        <div class="mt-4">
            <h3>Related Searches</h3>
            <textarea class="form-control" id="relatedSearches" rows="10" readonly></textarea>
        </div>

        <div class="mt-4">
            <h3>Page Text</h3>
            <textarea class="form-control" id="pageText" rows="10" readonly></textarea>
        </div>

        <div class="mt-4">
            <h3>Top Results</h3>
            <ul id="topResults" class="list-group"></ul>
        </div>
    </div>

    <script>
        $(document).ready(function() {
            let serpData;

            $('#search').click(function() {
                const keyword = $('#keyword').val();
                if (!keyword) {
                    alert('Please enter a keyword');
                    return;
                }
                performSearch(keyword);
            });

            function performSearch(keyword) {
                $.ajax({
                    url: '/search',
                    method: 'GET',
                    data: { keywords: keyword },
                    dataType: 'json',
                    success: function(response) {
                        if (response.error) {
                            alert('Error: ' + response.error);
                            return;
                        }
                        serpData = response;
                        $('#serpResults').val(JSON.stringify(response.search_results, null, 2));
                        $('#paaResults').val(JSON.stringify(response.paa, null, 2));
                        $('#relatedSearches').val(JSON.stringify(response.related_searches, null, 2));
                        $('#pageText').val(response.page_text.substring(0, 300) + '...'); // Display first 300 chars
                        displayTopResults(response.search_results);
                    },
                    error: function(xhr, status, error) {
                        alert('Error: ' + (xhr.responseJSON.error || error));
                    }
                });
            }

            function displayTopResults(results) {
                const $topResults = $('#topResults');
                $topResults.empty();
                results.forEach((result, index) => {
                    if (index < 20) {
                        $topResults.append(`<li class="list-group-item">
                            <h5>#${index+1} ${result.title}</h5>
                            <p>${result.snippet}</p>
                            <p><a href="${result.url}" target="_blank">${result.url}</a></p>
                        </li>`);
                    }
                });
            }

        });
    </script>
</body>
</html>
